package com.csg.ses.service.ses.impl;

import com.csg.ses.common.utils.CommonUtil;
import com.csg.ses.dao.ses.GroupInfoDao;
import com.csg.ses.entity.ses.GroupInfoEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import lombok.RequiredArgsConstructor;
import com.csg.ses.common.utils.MybatisPlusUtil;
import org.springframework.transaction.annotation.Transactional;
import com.csg.ses.vo.common.IdListVo;
import java.time.LocalDateTime;

import com.csg.ses.dao.ses.TopicInfoDao;
import com.csg.ses.entity.ses.TopicInfoEntity;
import com.csg.ses.service.ses.TopicInfoService;

/**
 * 
 * @author cjj
 * @date 2021-04-11 11:20:47
 */
@Service
@RequiredArgsConstructor
public class TopicInfoServiceImpl extends ServiceImpl<TopicInfoDao, TopicInfoEntity> implements TopicInfoService {
    private final TopicInfoDao topicInfoDao;
    private final GroupInfoDao groupInfoDao;

    @Override
    public Result retrieve(TopicInfoEntity topicInfo, Long current, Long size) {
        try {
            Page<TopicInfoEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<TopicInfoEntity> qw = new QueryWrapper();
            return new Result(topicInfoDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional
    public Result del(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                int num = topicInfoDao.deleteBatchIds(idList.getIdList());
                groupInfoDao.deleteBatchIds(idList.getIdList());
                return new Result(num + "条记录删除成功！", ResultStatus.SUCCESS);
            } else {
                return new Result("未指定需要删除的记录！", ResultStatus.FAILED);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(topicInfoDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public boolean save(TopicInfoEntity topicInfo) {
        try {
            if (!CommonUtil.isEmpty(topicInfo.getTopicId())) {
                topicInfoDao.updateById(topicInfo);

                GroupInfoEntity groupInfo = new GroupInfoEntity();
                groupInfo.setGroupId(topicInfo.getTopicId());
                groupInfo.setCollege(topicInfo.getCollege());
                groupInfo.setEmpClass(topicInfo.getEmpClass());
                groupInfo.setTopicTitle(topicInfo.getTopicTitle());
                groupInfo.setStuNum(topicInfo.getStuNum());
                groupInfo.setStuName(topicInfo.getStuName());
                groupInfo.setJsName(topicInfo.getJsName());
                groupInfoDao.updateById(groupInfo);

            } else {
                topicInfo.setTopicId(topicInfoDao.getNextId().toString());
                topicInfoDao.insert(topicInfo);

                GroupInfoEntity groupInfo = new GroupInfoEntity();
                groupInfo.setGroupId(topicInfo.getTopicId() + "");
                groupInfo.setCollege(topicInfo.getCollege());
                groupInfo.setEmpClass(topicInfo.getEmpClass());
                groupInfo.setTopicTitle(topicInfo.getTopicTitle());
                groupInfo.setStuNum(topicInfo.getStuNum());
                groupInfo.setStuName(topicInfo.getStuName());
                groupInfo.setJsName(topicInfo.getJsName());
                groupInfo.setApplyStep("未申请");
                Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2017-09-01");
                groupInfo.setStartDate(date);
                date = new SimpleDateFormat("yyyy-MM-dd").parse("2022-06-30");
                groupInfo.setEndDate(date);
                groupInfoDao.insert(groupInfo);

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result queryAllTopic() {
        return new Result(topicInfoDao.getAllTopic());
    }
}